<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use think\Controller;
use app\admin\model\Messages;
use app\admin\model\Users;
class Message extends Common{
    public function _initialize()
    {
        parent::_initialize();
        $this->Message = new Messages;
    }
    public function index(){
        $this->assign('is_red',1);
    	return $this->fetch();
    } 	
    public function unreads(){
        $this->assign('is_red',0);
    	return $this->fetch('index');
    }	
    public function getLists($page=null,$is_red=null,$key=null){
    	if($this->request->isPost()){
            $page = (int)$page > 0 ? $page : 1;
            $map=[];
            $map['touser'] = $this->User_id;  //上级客户
            if(is_numeric($is_red) && $is_red == 0){
                $map['is_red'] = (int) $is_red;
            }
            if(!empty($key)){
                $map['title'] = array('like',"%".$key."%");
            }
            $lists = $this->Message
                ->where($map)
                ->order('is_red asc, create_time desc')
                ->paginate(array('list_rows'=>$this->pageSize,'page'=>$page))
                ->toArray();
            return $result = ['code'=>0,'msg'=>lang('No Data...'),'data'=>$lists['data'],'count'=>$lists['total'],'rel'=>1];
        }
    }
    public function getNotice(){
        $map['touser'] = $this->User_id;
        $map['is_red'] = 0;
        $list = $this->Message->field("*,id as url")->where($map)->limit(5)->order('id desc')->select();
        if($list){
            $count = $this->Message->where($map)->count();
            return ['code'=>1,'msg'=>'','data'=>$list,'count'=>$count];
        }
        return ['code'=>0,'msg'=>'','data'=>''];
        //$count = $count > 100 ? '99+' : $count;
        
    }
    /*标记为已读*/
    public function reds($ids=null){
        if(empty($ids) || count($ids) <= 0){
            return $result = ['code'=>0,'msg'=>lang('Please select the data.')];
        }
        $map['id'] = array('in',$ids);
         $map['touser'] = $this->User_id;
        if($this->Message->where($map)->update(['is_red'=>1])){
            return $result = ['code'=>1,'msg'=>lang('All have been marked as read.')];
        }
        return $result = ['code'=>0,'msg'=>lang('The operation is wrong.')];
    }
    /*删除数据*/
    public function delete($ids=null){
        if(empty($ids) || count($ids) <= 0){
            return $result = ['code'=>0,'msg'=>lang('Please select the data.')];
        }
        $map['touser'] = $this->User_id;
        $map['id'] = array('in',$ids);
        if($this->Message->where($map)->delete()){
            return $result = ['code'=>1,'msg'=>lang('All of them have been deleted.')];
        }
        return $result = ['code'=>0,'msg'=>lang('The operation is wrong.')];
    }
    /*查看*/
    public function view($message_id=null){
        $mas['touser'] = $this->User_id;
        $mas['id'] = $message_id;
        $massge = $this->Message->where($mas)->find();
        if($massge){
            $massge->is_red = 1;
            $massge->save();
        }
        $this->assign('row',$massge);
        return $this->fetch();
    }
}